在 C++ 的舞台上,每个对象都有其生命周期—— 对象生命周期。它定义了对象在内存中占据空间的持续时间。在 函数体中, 局部变量 的默认行为是成为 自动,但我们可以命令它们变为 静态 以改变它们的命运。
1. 自动对象
默认情况下,局部变量是 自动对象。它们在 函数 执行到达其定义处时被创建(初始化),并在代码块结束时消亡(被回收)。它们位于栈上,因此每次调用都是全新的。
2. 局部静态对象
当你使用 静态 关键字时,你创建了一个 局部静态对象。这些对象仅在第一次控制流经过其定义前被初始化一次,并一直存活到程序终止。这使得函数可以在不污染全局作用域的情况下“记住”状态。
3. 递归陷阱
在 递归函数中,每一次 递归循环 都会为其自动对象创建一个独立实例。如果递归深度较大,将消耗大量栈空间。相反,一个 静态 对象会在该递归的每一层之间共享。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Where do automatic objects reside in memory?
The Heap
The Stack
The Static Data Segment
The Register
✅ Correct!
Automatic objects are local to the function execution and are stored on the stack.❌ Incorrect
The stack handles temporary local storage; the data segment is for globals and statics.QUESTION 2
How many times is a local static object initialized?
Every time the function is called.
Never; they are always zero-initialized.
Exactly once, before the first call execution reaches its definition.
Twice: once at compile-time and once at runtime.
✅ Correct!
Statics persist. The compiler ensures they are only initialized once throughout the program's life.❌ Incorrect
If it initialized every time, it wouldn't be 'static'—it would behave like an automatic object.QUESTION 3
What happens to a local static variable when the function returns?
It is destroyed and memory is freed.
It remains in memory and retains its value for the next call.
It becomes a global variable accessible by any function.
Its value is reset to the default constructor.
✅ Correct!
The object lifetime persists until the program ends, though its 'scope' remains local to the function.❌ Incorrect
While its lifetime is global, its visibility (scope) is still restricted to the function block.QUESTION 4
In a recursive function, what is the behavior of an automatic object?
A single instance is shared among all recursive calls.
A new instance is created for each call on the stack.
It is moved to the heap to prevent stack overflow.
It is converted to a static object by the compiler.
✅ Correct!
Each recursive call has its own stack frame, and thus its own local automatic variables.❌ Incorrect
Automatic objects are uniquely instantiated per function activation.QUESTION 5
Which keyword prevents 'global scope pollution' while still keeping variable state across calls?
extern
const
static
volatile
✅ Correct!
Local static objects provide persistence while hiding the variable inside the function scope.❌ Incorrect
The 'static' keyword within a function scope achieves local persistence.Exercise 7.3: Transaction Processing Revamped
Applying Class Members and Object Scope
You are tasked with revising the transaction-processing program from § 7.1.1. This exercise tests your ability to utilize class members like isbn() and combine() within the standard object lifecycle of a main function loop.
Q
Revise your transaction-processing program from § 7.1.1 (p. 256) to use these members. Provide the main logic implementation.
Solution:
To revise the program, we use the Sales_data members inside the loop to aggregate data. Model solution: cpp Sales_data total; if (read(cin, total)) { Sales_data trans; while(read(cin, trans)) { if (total.isbn() == trans.isbn()) total.combine(trans); else { print(cout, total) << endl; total = trans; } } print(cout, total) << endl; } Here, 'total' and 'trans' are automatic objects created within the main scope/loop.
To revise the program, we use the Sales_data members inside the loop to aggregate data. Model solution: cpp Sales_data total; if (read(cin, total)) { Sales_data trans; while(read(cin, trans)) { if (total.isbn() == trans.isbn()) total.combine(trans); else { print(cout, total) << endl; total = trans; } } print(cout, total) << endl; } Here, 'total' and 'trans' are automatic objects created within the main scope/loop.